package com.lcw.wechatserverconnet.Controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: lcw
 * @date 2020/5/14 18:46
 */
@RestController
@RequestMapping(value = "/serverConnect")
public class ServerConnect {

  private Logger logger = LoggerFactory.getLogger(ServerConnect.class);

  @GetMapping(value = "/wxServerReturn")
  public String wxServerReturn(HttpServletRequest request,
      @RequestParam(required = false) String echostr,
      @RequestParam(required = false) String signature,
      @RequestParam(required = false) String timestamp,
      @RequestParam(required = false) String nonce
  ) {
    try {
      //只需要把微信请求的 echostr, 返回给微信就可以了
      logger.info("测试来过===================" + echostr);
      logger.info("测试来过===================" + signature);
      logger.info("测试来过===================" + timestamp);
      logger.info("测试来过===================" + nonce);
      return echostr;
    } catch (Exception e) {
      logger.info("测试微信公众号的接口配置信息发生异常：", e);
      return "错误！！！";
    }

  }

  //
  @PostMapping(value = "/wxPostServerRequestUrlDisplay")
  public String wxServerPostReturn(HttpServletRequest request,@RequestBody String body) {
    //打印请求http
    String uri = request.getRequestURI();
    String url = uri.substring(request.getContextPath().length());
    logger.info("http之uri： "+ uri);
    logger.info("http之url： "+ url);
    logger.info("http request method: "+ request.getMethod());
    Map map = request.getParameterMap();
    String signature = request.getParameter("signature");
    String timestamp = request.getParameter("timestamp");
    String nonce = request.getParameter("nonce");
    String encrypt_type = request.getParameter("encrypt_type");
    String msg_signature = request.getParameter("msg_signature");
    logger.info("http request body: "+ body);
    return "Success";
  }
}
